import App from './App.vue'
import pinia from '@/framework/store'
import router, { enterDynamicRoute, setEnableEnterFirstDynamicRoute } from '@/framework/router'
import { createApp } from 'vue'
import STable from '@surely-vue/table'
import '@/framework/assets/css/base.css'
import VueLazyload from 'vue-lazyload'
import '@surely-vue/table/dist/index.less'
import pluginLoader from '@/framework/plugin'
import { download, upload } from '@/framework/network/request'
// 自己封装的，用于显示antd Menu的图标组件
import { setupAntdIcon } from '@/framework/components/common/antdIcons/AntdIcons'
import defaultAvatar from '@/framework/assets/imgs/defaultAvatar.png'
import { useCommonStore } from '@/framework/store/common'
import { checkLoginState } from '@/framework/network/login'
// 数据大屏
import DataVVue3 from '@kjgl77/datav-vue3'
import 'echarts-liquidfill'
import * as echarts from 'echarts' // 引入echarts
// 引入字体
import './assets/fonts/fonts.css'

const app = createApp(App)
// 自定义app.use
pluginLoader(app)
// 因为在使用routeStore之前，需要初始化pinia，所以需要把这句话单独拿出来
app.use(pinia)
app.use(router)
app.use(STable)
app.use(VueLazyload, {loading: defaultAvatar})
app.use(DataVVue3)
app.provide('upload', upload)
app.provide('download', download)
setupAntdIcon(app)
app.mount('#app')
app.config.globalProperties.$echarts = echarts // 全局使用
// const enableEnterFirstDynamicRoute = window.location.href.indexOf("front.bidr.com.cn/erp/") !== -1 || window.location.href.indexOf("localhost") !== -1
setEnableEnterFirstDynamicRoute(true)
router.beforeEach(async (to, from, next) => {
  const commonStore = useCommonStore(pinia)
  if (commonStore.hasLogin || to.path === '/login') {
    enterDynamicRoute(to, from, next)
  } else {
    await checkLoginState()
    delete to.query.id_token
    next({path: to.path, query: to.query})
  }
})

export default app
